export class PropType{
    public static readonly NUMBER:PropType=new PropType(0,"数字","number");
    public static readonly STRING:PropType=new PropType(1,"字符串","string");
    public static readonly ARRAY:PropType=new PropType(2,"数组","any[]");
    public static readonly OBJECT:PropType=new PropType(3,"对象","any");       
    public static readonly ARRAY2:PropType=new PropType(4,"二维数组","any[][]");
    public static readonly INT:PropType=new PropType(5,"整型", "int");
    public static readonly UINT:PropType=new PropType(6,"无符号整型","uint");
    public static readonly BOOL:PropType=new PropType(7,"布尔类型","boolean");
    public static readonly ARRAY_OBJECT:PropType=new PropType(8,"对象数组","any[]"); 
    public static readonly ARRAY_NUMBER:PropType=new PropType(9,"数字数组","number[]");
    public static readonly ARRAY_STRING:PropType=new PropType(10,"文本数组","string[]");
    public static readonly typeList:PropType[] = [PropType.NUMBER,PropType.STRING,
        PropType.ARRAY,PropType.OBJECT,PropType.ARRAY2,PropType.INT,PropType.UINT,PropType.BOOL,PropType.ARRAY_OBJECT,PropType.ARRAY_NUMBER,PropType.ARRAY_STRING];
    public static getType(id:number):PropType
    {
        return PropType.typeList[id];
    }

    // public static readonly ARRAY_PAIR:PropType=new PropType(100,"ly.Pair数组");
    public _name:string;
    public _id:number;
    public _key:string;
    constructor(id:number,name:string,key:string)
    {
        let s = this;
        s._id = id;
        s._name = name;
        s._key = key;
    }
    public typeToStr(val:any):string
    {
        let s= this;
        let str:string;
        if(s == PropType.NUMBER)
            return val == null?"0":String(val);
        if(s == PropType.INT)
            return String(Math.floor(val));
        if(s == PropType.UINT)
            return String(Math.abs(val));
        if(s == PropType.STRING)
            return val==null?"":String(val);
        if(s == PropType.ARRAY || s == PropType.ARRAY_NUMBER || s == PropType.ARRAY_STRING)
            return val?val.toString():"";        
        if(s == PropType.ARRAY_STRING)
        {
            val = val?val.split(","):[];
            return val;
        }
        if(s == PropType.BOOL)
            return val && val!="0"?"true":"false";
        if(s == PropType.ARRAY2)            
        {
            if(val == null)return "";
            str = val.toString();
            return str.substr(1,str.length - 2);
        }
        
        if(s == PropType.OBJECT || s == PropType.ARRAY_OBJECT)
        {
            if(val.contructor == String)
                return val;
            return JSON.stringify(val);
        }
        return "";
    }
    public strToValue(val:any):any
    {
        let s= this;
        if(s == PropType.NUMBER)
            return Number(val);
        if(s == PropType.INT)
            return val|0;
        if(s == PropType.UINT)
            return Math.abs(val|0);
        if(s == PropType.STRING)
            return val==null?"":String(val);
        if(s == PropType.BOOL)
            return val==="true" || val=="1"?true:false;
        if(s == PropType.ARRAY || s == PropType.ARRAY_STRING)
            return val?val.split(","):[];
        if(s == PropType.ARRAY_NUMBER)
        {
            if(val)
            {
                val = PropType.vectorToNumberArray(val.split(","));                
                return val;
            }
            else
                return [];
        }
        if(s == PropType.ARRAY2)
            return s.parse("["+(val?val:"")+"]");
        if(s == PropType.OBJECT || s == PropType.ARRAY_OBJECT)
            return s.parse(val);
            
    }
    public parse(val:string):any
    {
        try
        {
            return JSON.parse(val);
        }
        catch(e)
        {                
            let s = this;
            return s == PropType.ARRAY2 || s== PropType.ARRAY_OBJECT?[]:null;   
        }
        
        
    }
    public isArray():boolean
    {
        let s = this;
        return s == PropType.ARRAY2 || s == PropType.ARRAY_OBJECT || s == PropType.ARRAY;
    }

    public isNumber():boolean
    {
        let s = this;
        return s == PropType.UINT || s == PropType.INT || s == PropType.NUMBER;
    }

    /**数组转成数值数组*/
    public static vectorToNumberArray(vec:Array<any>):Array<any>
    {
        var i :number,len :number;
        var arr:Array<number>=[];
        len = vec.length;
        for(i=0;i<len;++i)
        {
            arr[arr.length] = +vec[i];
        }
        return arr;
    }
}